<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>字典管理</el-breadcrumb-item>
      <el-breadcrumb-item>首页轮播图管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-alert title="添加修改会删除缓存，请谨慎操作" type="warning" show-icon>
      </el-alert>

      <el-row class="layoutMenu" :gutter="20">
        <el-col :span="4">
          <el-button type="primary" @click="addLayoutCarouselShow"
            >添加首页轮播图</el-button
          >
        </el-col>
      </el-row>

      <el-table :data="layoutCarouselList" border stripe>
        <el-table-column type="index" label=""></el-table-column>
        <el-table-column label="图片">
          <template slot-scope="scope">
            <!-- <img :src="scope.row.url"> -->
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.url"
              fit="fill"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="priority" label="排序"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              icon="el-icon-delete"
              type="danger"
              @click="delLayoutCarouseById(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      title="图片展示"
      :visible.sync="previewDialogVisible"
      @close="previewDialogclose"
      width="80%"
    >
      <img :src="previewPath" class="previewImg" />
    </el-dialog>

    <el-dialog
      title="添加首页轮播图"
      :visible.sync="addDialogVisible"
      @close="addDialogclose"
      width="50%"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="图片:" label-width="100px">
          <el-upload
            ref="updateImageRef"
            class="upload-demo"
            :headers="headObj"
            :action="updateImgUrl"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :on-success="handleSuccess"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>

        <el-form-item label="优先级:" prop="priority" label-width="100px">
          <el-input v-model.number="addForm.priority" class="inp"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addDialogSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// import { getLayoutMenuData, addLayoutMenuData, getLayoutMenuDataById, updateLayoutMenuData, deleteLayoutMenuDataById } from '@/api/layoutMenu'
import { UploadImgDel } from '@/api/uploadImg'
import { addLayoutCarousel, getLayoutCarouselData, delLayoutCarouseById } from '@/api/layoutCarousel'
export default {
  name: 'LayoutCarousel',
  data () {
    return {
      updateImgUrl: 'http://192.168.10.1:6002/admin/dictionary/file/UploadImg',
      headObj: {
        AdminAuthorizationToken: this.$store.getters.token
      },
      layoutCarouselList: [],
      previewDialogVisible: false,
      previewPath: '',
      picArr: [],
      addDialogVisible: false,
      addForm: {
        url: '',
        priority: 0
      },
      addFormRules: {
        priority: [
          { required: true, message: '请输入优先级', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async getLayoutCarouselList () {
      const res = await getLayoutCarouselData()
      this.layoutCarouselList = res.data
    },
    addLayoutCarouselShow () {
      this.addDialogVisible = true
    },
    handlePreview (file) {
      this.previewPath = file.response.data
      this.previewDialogVisible = true
    },
    previewDialogclose () {},
    async handleRemove (file) {
      const res = await UploadImgDel(file.response.data)
      this.$message.success(res.data)
      const index = this.picArr.findIndex((item) => item === file.response)
      this.picArr.splice(index, 1)
    },
    handleSuccess (response) {
      this.$message.success('上传成功')
      //   console.log(response.data)
      this.picArr.push(response.data)
    },
    addDialogclose () {
      this.addDialogVisible = false
      this.$refs.updateImageRef.clearFiles()
      this.picArr = []
      this.$refs.addFormRef.resetFields()
    },
    addDialogSubmit () {
      if (this.picArr.length !== 1) {
        this.$message.error('只能上传一张图片')
        return
      }
      console.log(this.picArr)
      console.log(this.picArr[0])
      this.addForm.url = this.picArr[0]
      console.log(1)
      console.log(this.addForm.url)
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return false
        const res = await addLayoutCarousel(this.addForm)
        this.$message.success(res.data)
        this.addDialogVisible = false
        this.getLayoutCarouselList()
      })
    },
    async delLayoutCarouseById (id) {
      const res = await delLayoutCarouseById(id)
      this.$message.success(res.data)
      this.getLayoutCarouselList()
    }
  },
  created () {
    this.getLayoutCarouselList()
  }
}
</script>

  <style>
.layoutMenu {
  margin-top: 20px;
}
.previewImg {
  width: 100%;
}
</style>
